// pages/test-manage/test-manage.js
var util = require('../../utils/util.js')
const DB = wx.cloud.database()
//获取应用实例
const app = getApp()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    //处理导 航 栏的高度
    rpxdata: wx.getStorageSync('rpxdata'),
    navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 'px',
    navigationBarAndStatusBarAndsearchHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 60 + 'px',
    //设置导航栏的显示信息
    navbarData: {
      showCapsule: 1,
      title: "待还确认"
    },
    //按钮颜色
    manger: 0,
    DB_book: {}
  },

  refresh(){
    let that = this
    DB.collection("lchbook").where({
      hsstate: true
    }).get({success:(res)=>{that.setData({DB_book:res.data})  }  })
  },

  manger(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    let stock = e.currentTarget.dataset.stock
    let jynum = e.currentTarget.dataset.jynum
    
    if (this.data.manger == 0) {
      wx.showToast({
        title: '需要管理员',
        image: '/images/fail.png',
        duration: 2000
      })
    } else {    
        DB.collection("lchbook").doc(id).update({
            data: {
              jyren: "",
              jytime: "",
              jydate: "",
              openid: '',
              jystate: false,
              hsstate: false,
              stock: stock + 1,
              jynum: jynum + 1
          },
          success: (res) => {
            wx.showToast({
              title: '确认成功',
              icon: 'success',
              duration: 2000
            })
            DB.collection("lchbook").where({
              hsstate: true
            }).get({success:(res)=>{that.setData({DB_book:res.data})  }  })
          },
          fail: (res) => {
            wx.showToast({
              title: '数据库失败',
              image: '/images/fail.png',
              duration: 2000
            })
          }
        })
       
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    this.setData({
      openid: wx.getStorageSync('openid')
    })

    //获取用户是否管理员 
    DB.collection("lchuser").where({
      openid: this.data.openid
    }).get({
      success: (res) => {
        that.setData({
          manger: res.data[0].manger
        })
      }
    })

    //获取待还书籍信息
    DB.collection("lchbook").where({
      hsstate: true
    }).get({success:(res)=>{that.setData({DB_book:res.data})   }  })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})